package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.request.PostionAddListRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InvPostionMapper;
import com.ruoyi.system.domain.InvPostion;
import com.ruoyi.system.service.IInvPostionService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 持仓Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-12
 */
@Service
public class InvPostionServiceImpl implements IInvPostionService {

    @Autowired
    private InvPostionMapper invPostionMapper;

    /**
     * 查询持仓
     *
     * @param id 持仓主键
     * @return 持仓
     */
    @Override
    public InvPostion selectInvPostionById(Long id) {
        return invPostionMapper.selectInvPostionById(id);
    }

    /**
     * 查询持仓列表
     *
     * @param invPostion 持仓
     * @return 持仓
     */
    @Override
    public List<InvPostion> selectInvPostionList(InvPostion invPostion) {
        return invPostionMapper.selectInvPostionList(invPostion);
    }

    /**
     * 新增持仓
     *
     * @param invPostion 持仓
     * @return 结果
     */
    @Override
    public int insertInvPostion(InvPostion invPostion) {
        return invPostionMapper.insertInvPostion(invPostion);
    }

    @Override
    public int insertInvPostionList(PostionAddListRequest invPostion) {
        invPostionMapper.deleteInvPostionByAccountId(invPostion.getAccountId());
        List<String> splitLine = Arrays.asList(invPostion.getInvPostion().split("\n"));
        List<InvPostion> data = new ArrayList<>();
        //同花顺
        if (invPostion.getType()==0){
            tongHua(splitLine,invPostion);
        }else  if (invPostion.getType()==1){
            // 华宝
            HuaBao(splitLine,invPostion);
        }
        return 1;

    }

    private void HuaBao(List<String> splitLine, PostionAddListRequest invPostion) {
        for (String line : splitLine) {
            List<String> strings = Arrays.asList(line.split("   ")).stream().map(u->u.trim())
                    .filter(u -> StringUtils.isNotEmpty(u)).collect(
                            Collectors.toList());
            if (CollectionUtils.isEmpty(strings)) {
                continue;
            }
            String code = strings.get(0);
            if (!NumberUtils.isDigits(code)) {
                continue;
            }
            InvPostion pos = new InvPostion();
            pos.setSecCode(code);
            pos.setSecName(strings.get(1));
            pos.setSecBalance(Long.parseLong(strings.get(2)));
            pos.setSecAvailable(Long.parseLong(strings.get(3)));
            pos.setSecCost(new BigDecimal(strings.get(4)));
           pos.setSecMarket(new BigDecimal(strings.get(5)));
            pos.setSecAccount(invPostion.getAccountId());
//            pos.setSecDateNum(
//                    NumberUtils.isDigits(strings.get(10)) ? Long.parseLong(strings.get(10)) : 0L);
            invPostionMapper.insertInvPostion(pos);
        }
    }

    private void tongHua(List<String> splitLine, PostionAddListRequest invPostion) {
        for (String line : splitLine) {
            List<String> strings = Arrays.asList(line.split("\t")).stream()
                    .filter(u -> StringUtils.isNotEmpty(u)).collect(
                            Collectors.toList());
            if (CollectionUtils.isEmpty(strings)) {
                continue;
            }
            String code = strings.get(0);
            if (!NumberUtils.isDigits(code)) {
                continue;
            }
            InvPostion pos = new InvPostion();
            pos.setSecCode(code);
            pos.setSecName(strings.get(1));
            pos.setSecBalance(Long.parseLong(strings.get(2)));
            pos.setSecAvailable(Long.parseLong(strings.get(3)));
            pos.setSecCost(new BigDecimal(strings.get(4)));
            pos.setSecMarket(new BigDecimal(strings.get(5)));
            pos.setSecAccount(invPostion.getAccountId());
            pos.setSecDateNum(
                    NumberUtils.isDigits(strings.get(10)) ? Long.parseLong(strings.get(10)) : 0L);
            invPostionMapper.insertInvPostion(pos);
        }
    }


    /**
     * 修改持仓
     *
     * @param invPostion 持仓
     * @return 结果
     */
    @Override
    public int updateInvPostion(InvPostion invPostion) {
        return invPostionMapper.updateInvPostion(invPostion);
    }

    /**
     * 批量删除持仓
     *
     * @param ids 需要删除的持仓主键
     * @return 结果
     */
    @Override
    public int deleteInvPostionByIds(String ids) {
        return invPostionMapper.deleteInvPostionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除持仓信息
     *
     * @param id 持仓主键
     * @return 结果
     */
    @Override
    public int deleteInvPostionById(Long id) {
        return invPostionMapper.deleteInvPostionById(id);
    }
}
